using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Threading;

using SqlWebAdmin.CSharp;

public partial class StoredProcedures : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
            lblError.Visible = false;
            dgStoredProcedures.Visible = false;


            string databaseName = Request.QueryString["database"];


			Server smoServer = SmoHelper.GetServer();
            try
            {
                

                Database smoDatabase = smoServer.Databases[databaseName];
                if (smoDatabase != null)
                {
                    smoServer.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject", "CreateDate");

					smoDatabase.StoredProcedures.Refresh();
                    StoredProcedureCollection smoStoredProcedures = smoDatabase.StoredProcedures;
					

                    lblNoStoredProcedures.Visible = false;
                    if (smoStoredProcedures.Count > 0)
                    {

						List<StoredProcedure> spList = null;
 
						if (ddlFilter.SelectedValue == "Show system stored procedures only")
						{
							spList = SmoHelper.FilterSystemObjects(smoDatabase.StoredProcedures, true);
						}
						else if(ddlFilter.SelectedValue == "Show user stored procedures only")
						{
							spList = SmoHelper.FilterSystemObjects(smoDatabase.StoredProcedures, false);
						}

                        dgStoredProcedures.DataSource = spList;
                        dgStoredProcedures.DataBind();

                        dgStoredProcedures.Visible = true;
                    }
                    else
                    {
                        lblNoStoredProcedures.Visible = true;
                    }

                }
                else
                {
                    Response.Redirect(String.Format("error.aspx?error={0}", Globals.databaseNotFoundErrorNumber), false);
                }
            }
            catch (Microsoft.SqlServer.Management.Common.ExecutionFailureException execEx)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(execEx));
                lblError.Visible = true;
            }
            catch (SmoException smoEx)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                lblError.Visible = true;
            }
            catch (System.Exception ex)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                lblError.Visible = true;
            }
        
    }

    protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnAddNewStoredProcedure_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administration/CreateStoredProcedure.aspx?database=" + HttpUtility.UrlEncode(Request.QueryString["database"]), false);
    }
}

